package com.zb.order.controller;


import com.zb.entity.AccounLoginInfo;
import com.zb.entity.ExportFieldVo;
import com.zb.order.service.ConWorkExportFieldSerivce;
import com.zb.result.PageUtil;
import com.zb.result.Result;
import com.zb.service.ZjWorkExportFieldService;
import com.zb.service.ZjWorkFieldService;
import com.zb.service.ZjWorkStatementTemplateFieldService;
import com.zb.utils.TokenUtil;
import com.zb.vo.WorkExportAndSaveFieldVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * <p>
 * 工单导出字段表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2024-03-04
 */
@Api(tags = "工单导出字段类接口")
@RestController
@RequestMapping("/work/export/field")
public class ZjWorkExportFieldController {
    @Resource
    private ConWorkExportFieldSerivce conWorkExportFieldSerivce;

    @Resource
    private ZjWorkExportFieldService workExportFieldService;
    @Resource
    private ZjWorkFieldService workFieldService;
    @Resource
    private ZjWorkStatementTemplateFieldService workStatementTemplateFieldService;

    @ApiOperation("获取工单系统初始导出字段")
    @GetMapping("/getOrderSysExportFields")
    public Result getOrderSysInitExportFields() {
        return Result.success("获取工单系统初始导出字段成功").data(PageUtil.rows(workExportFieldService.getOrderSysInitExportFields()));
    }

    @ApiOperation("获取工单自定义字段")
    @GetMapping("/getOrderCustomField")
    public Result getOrderCustomField(HttpServletRequest request) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取工单自定义字段成功").data(PageUtil.rows(workFieldService.getOrderCustomField(custAccId)));
    }

    @ApiOperation("获取工单结单导出字段")
    @GetMapping("/getOrderSettlementField")
    public Result getOrderSettlementField(HttpServletRequest request) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取结单字段成功").data(PageUtil.rows(workStatementTemplateFieldService.getStatementTemlateField(custAccId)));
    }

    @ApiOperation("获取工单所有导出字段")
    @GetMapping("/getOrderExportField")
    public Result getOrderExportFields(HttpServletRequest request) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        return Result.success("获取工单导出字段成功").data(PageUtil.rows(workExportFieldService.getOrderExportFields(loginInfo)));
    }

    @ApiOperation("保存导出字段")
    @PostMapping("/save")
    public Result save(HttpServletRequest request, @RequestBody @Valid ExportFieldVo exportFieldVo) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        workExportFieldService.save(loginInfo, exportFieldVo);
        return Result.success("保存导出字段成功");
    }

    @ApiOperation("保存并导出字段")
    @PostMapping("/saveAndExportField")
    public void saveAndExportField(HttpServletRequest request, HttpServletResponse response, @RequestBody @Valid WorkExportAndSaveFieldVo exportFieldVo) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        workExportFieldService.save(loginInfo, exportFieldVo.getExportFieldVo());
        conWorkExportFieldSerivce.orderExportField(response, loginInfo, exportFieldVo);
    }



}
